﻿using BaseData.API.Read.Applications.DoctorScheduling.Command;
using BaseData.API.Read.Dtos.DoctorSchedulingDto;
using BaseData.ErrorCode;
using BaseData.Domain.DoctorScheduling;

using BaseData.Domain.HospitalInformation;
using BaseData.Infrastructure.Interfaces;
using MediatR;

namespace BaseData.API.Read.Applications.DoctorScheduling.CommandHandler
{
    /// <summary>
    /// 医生排班列表
    /// </summary>
    public class DoctorSchedulingCommandHandler : IRequestHandler<DoctorSchedulingCommand, ApiResult<ApiPaging<DoctorSchedulingDto>>>
    {
        private readonly IBaseRepository<Scheduling> schbaseRepository;
        private readonly IBaseRepository<Departments> deptbaseRepository;
        private readonly IBaseRepository<Doctors> doctbaseRepository;
        //测色
        public DoctorSchedulingCommandHandler(IBaseRepository<Departments> deptbaseRepository, IBaseRepository<Scheduling> schbaseRepository, IBaseRepository<Doctors> doctbaseRepository)
        {
            this.schbaseRepository = schbaseRepository;
            this.deptbaseRepository = deptbaseRepository;
            this.doctbaseRepository = doctbaseRepository;
        }

        public Task<ApiResult<ApiPaging<DoctorSchedulingDto>>> Handle(DoctorSchedulingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<DoctorSchedulingDto>> result = new ApiResult<ApiPaging<DoctorSchedulingDto>>();
            result.Code = ApiEnum.查询成功;
            result.Message=ApiEnum.查询成功.ToString();
            var list = from a in schbaseRepository.GetAll()
                       join b in deptbaseRepository.GetAll()
                       on a.DepetmentId equals b.DepartmentId
                       join c in doctbaseRepository.GetAll()
                       on a.DoctorId equals c.DoctorId
                       select new DoctorSchedulingDto
                       {
                           SchedulingId=a.SchedulingId,
                           DoctorName=c.DoctorName,
                           DoctorId=c.DoctorId,
                           StartDate=a.StartDate,
                           Session=a.Session,
                           ConsultationTime=a.ConsultationTime,
                           DepetmentId=b.DepartmentId,
                           DepartmentName=b.DepartmentName,
                           Num=a.Num,
                           PatientNum=a.PatientNum,
                           SchedulingState=a.SchedulingState,
                       };
            int tocom=list.Count();
            int topage = (int)Math.Ceiling(1.0 * tocom / request.PageSize);
            list = list.OrderBy(x => x.SchedulingId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            if (request.DepetmentId != null)
            {
                list = list.Where(x => x.DepetmentId == (request.DepetmentId));
            }
            if (!string.IsNullOrEmpty(request.DoctorName))
            {
                list = list.Where(x => x.DoctorName.Contains(request.DoctorName));

            }
            ApiPaging<DoctorSchedulingDto> page=new ApiPaging<DoctorSchedulingDto>();
            page.TotalCount = tocom;
            page.PageCount = topage;
            page.Data=list.ToList();
            result.Data = page;
            return Task.FromResult(result);


        }
    }
}
